package com.jdjw.modules.buscomplaintaccept.controller;


import cn.hutool.core.bean.BeanUtil;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.buscapcomplaint.entity.BusCapComplaint;
import com.jdjw.modules.buscapcomplaint.service.BusCapComplaintService;
import com.jdjw.modules.buscomplaint.entity.BusComplaint;
import com.jdjw.modules.buscomplaint.service.BusComplaintService;
import com.jdjw.modules.buscomplaintaccept.entity.BusComplaintAccept;
import com.jdjw.modules.buscomplaintaccept.entity.BusComplaintAcceptVo;
import com.jdjw.modules.buscomplaintaccept.service.BusComplaintAcceptService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 群众投诉受理办理表 前端控制器
 * </p>
 *
 * @author fq
 * @since 2021-06-10
 */
@RestController
@RequestMapping("/buscomplaintaccept")
public class BusComplaintAcceptController {


    @Autowired
    private BusComplaintAcceptService busComplaintAcceptService;

    @Autowired
    private BusComplaintService busComplaintService;

    @Autowired
    private BusCapComplaintService busCapComplaintService;

    @ApiOperation("群众投诉受理数据推送")
    @PostMapping("/save")
    public ResponseResult save(BusComplaintAcceptVo busComplaintAcceptVo){
        try {
            BusComplaintAccept busComplaintAccept=new BusComplaintAccept();
            BeanUtils.copyProperties(busComplaintAcceptVo,busComplaintAccept);
            busComplaintAcceptService.save(busComplaintAccept);//添加投诉流程记录
            //通过id获取到投诉数据，进行修改状态
            BusComplaint byId = busComplaintService.getById(busComplaintAccept.getComplaintId());
            if(byId!=null){
                if(busComplaintAccept.getAcceptStatus().equals("5")){
                    byId.setFlag("2");
                }
                byId.setAcceptStatus(busComplaintAccept.getAcceptStatus());
                busComplaintService.updateById(byId);//修改
            }
            BusCapComplaint byId1 = busCapComplaintService.getById(busComplaintAccept.getComplaintId());
            if(byId1!=null){
                if(busComplaintAccept.getAcceptStatus().equals("5")){
                    byId1.setFlag("2");
                    busCapComplaintService.updateById(byId1);
                }
            }
            return new ResponseResult(200,"操作成功",null);
        } catch (BeansException e) {
            e.printStackTrace();
            return new ResponseResult(500,"操作失败",null);
        }
    }

    @ApiOperation("查看投诉流程")
    @PostMapping("{id}/preview")
    @ResponseBody
    public ResponseResult preview(@PathVariable("id")String id){
        ResponseResult rrt=new ResponseResult();
        try {
            Map<String,Object> map=new HashMap<>();
            map.put("complaint_id",id);
            Collection<BusComplaintAccept> busComplaintAccepts = busComplaintAcceptService.listByMap(map);

            System.out.println(busComplaintAccepts);
//            List<BusComplaintAccept> busComplaintAccepts =(List<BusComplaintAccept>) busComplaintAcceptService.listByMap(map);
            rrt.setCode(200);
            rrt.setMsg("查看成功");
            rrt.setData(busComplaintAccepts);
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setCode(500);
            rrt.setMsg("查看失败");
            rrt.setData(null);
        }
        return rrt;
    }

}

